package com.quanquan.store.controller;

import DTO.PageDTO;
import base.BaseController;
import com.quanquan.store.dto.ActivityApplyDTO;
import com.quanquan.store.request.ActivityApplyRequestBody;
import com.quanquan.store.service.ActivityApplyService;
import enums.ResCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import web.response.ResultBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 功能描述:
 *
 * @ClassName: ActivityApplyController
 * @Author: xiaoqianh丶
 * @Date: 2019-06-27 14:03
 * @Version: V1.0
 */
@RestController
@RequestMapping("/activity_apply")
public class ActivityApplyController extends BaseController {

    @Autowired
    private ActivityApplyService activityApplyService;


    /**
     * 活动报名
     *
     * @param requestBody
     * @return
     */
    @PostMapping("/add")
    public ResultBody createApply(@RequestBody ActivityApplyRequestBody requestBody) {

        String userId = requestBody.getUserId();
        String activityId = requestBody.getActivityId();

        String msg = activityApplyService.createApplyRecord(activityId, userId);
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(),msg, msg);
    }

    /**
     * 后端查询活动报名用户
     * @param requestBody
     * @return
     */
    @PostMapping("/query")
    public ResultBody findAllApplyByActivityId(@RequestBody ActivityApplyRequestBody requestBody) {
        String activityId = requestBody.getActivityId();
        Integer page = requestBody.getPage() == null ? 0 : requestBody.getPage();
        Integer size = requestBody.getSize() == null ? 100 : requestBody.getSize();
        String keyword = requestBody.getKeyword();
        PageDTO<ActivityApplyDTO> allActivityApplys = activityApplyService.findAllActivityApplys(page, size, activityId, keyword);

        if (allActivityApplys == null) {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMsg());

        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), allActivityApplys);


    }

    /**
     * 查询每日报表
     *
     * @param requestBody
     * @return
     */
    @PostMapping("/backend_query")
    @ResponseBody
    public Map<String, Object> getAll(ActivityApplyRequestBody requestBody) {
        String activityId = requestBody.getActivityId();
        Integer page = requestBody.getPage();
        if (null != page && page > 0) {
            page = page -1;
        }
        Integer size = requestBody.getSize() == null ? 1000 : requestBody.getSize();
        String keyword = requestBody.getKeyword();
        PageDTO<ActivityApplyDTO> pageDTO = activityApplyService.findAllActivityApplys(page, size, activityId, keyword);
        Map<String, Object> data = new HashMap<>();
        if (null != pageDTO) {
            data.put("size",pageDTO.getPageSize());
            data.put("rows",pageDTO.getList());
            data.put("msg","success!");
            data.put("status",200);
            data.put("total",pageDTO.getTotal());
        } else {
            data.put("size",10);
            data.put("rows",null);
            data.put("msg","no data!");
            data.put("status",-200);
            data.put("total",0);
        }
        return data;
    }

    @PostMapping("/find_all")
    @ResponseBody
    public ResultBody getAllApply(@RequestBody ActivityApplyRequestBody requestBody){
        PageDTO<ActivityApplyDTO> allApply = activityApplyService.findAllApply(requestBody);
        if (allApply == null) {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMsg());
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), allApply);
    }
}
